package com.asura.qqeffect.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;

/**
 * Created by Asura on 2016/12/7.
 */

public class EasySlidingMenu extends HorizontalScrollView {

    private View menu;
    private View content;
    private int menuWidth;
    private int screenWidth;
    private VelocityTracker velocityTracker;
    private int touchSlop;
    private int maximumFlingVelocity;
    private int minimumFlingVelocity;

    public EasySlidingMenu(Context context) {
        this(context, null);
    }

    public EasySlidingMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EasySlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    private void init(Context context) {
        screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        velocityTracker = VelocityTracker.obtain();
        ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
        touchSlop = viewConfiguration.getScaledTouchSlop();
        maximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity();
        minimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        velocityTracker.clear();
        velocityTracker.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        menu = ((ViewGroup) getChildAt(0)).getChildAt(0);
        content = ((ViewGroup) getChildAt(0)).getChildAt(1);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        content.getLayoutParams().width = screenWidth;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        menuWidth = menu.getMeasuredWidth();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            scrollTo(menuWidth, 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        velocityTracker.addMovement(ev);
        switch (ev.getAction()){
            case MotionEvent.ACTION_UP:
                velocityTracker.computeCurrentVelocity(100);
                float xVelocity = velocityTracker.getXVelocity();
                float yVelocity = velocityTracker.getYVelocity();
                int scrollX = getScrollX();
                if(scrollX >= menuWidth/2){
                    smoothScrollTo(menuWidth,0);
                }else {
                    smoothScrollTo(0,0);
                }
                return true;
        }
        return super.onTouchEvent(ev);
    }

}
